﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Text;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    [Header("监听事件")]
    public SceneLoadEventSO sceneloadEvent;
    public VoidEventSO afterSceneLoadedEvent;
    public VoidEventSO loadDataEvent;
    public VoidEventSO backToMenuEvent;

    public PlayerInputController inputControl;//通过unity的inputsystem创建的c#类来实例化对象
    public Vector2 inputDirection;//二维向量，移动方向，玩家移动需要
    public float speed;//浮点数速度，用来实现人物移动
    public float jumpForce;//跳跃的力
    private Rigidbody2D rb;//刚体
    private CapsuleCollider2D coll;
    private PlayerAnimation playerAnimation;

    private Animator anim;

    private PhysicsCheck physicsCheck;

    public bool isHurt;
    public bool isDead;
    public bool isAttack;
    public float hurtForce;

    [Header("物理材质")]
    public PhysicsMaterial2D normal;
    public PhysicsMaterial2D wall;

    private void Awake()
    {   
        rb = GetComponent<Rigidbody2D>();//获取自身刚体组件
        inputControl = new PlayerInputController();//实例化对象
        coll = GetComponent<CapsuleCollider2D>();
        playerAnimation = GetComponent<PlayerAnimation>();
        physicsCheck = GetComponent<PhysicsCheck>();

        inputControl = new PlayerInputController();

        inputControl.Gameplay.Jump.started += Jump;
        inputControl.Gameplay.Attack.started += Attack;

        inputControl.Enable();
    }

    private void OnEnable()
    {
        
        sceneloadEvent.LoadRequestEvent += OnLoadEvent;
        afterSceneLoadedEvent.onEventRaised += OnAfterSceneLoadedEvent;
        loadDataEvent.onEventRaised += OnLoadDataEvent;
        backToMenuEvent.onEventRaised += OnLoadDataEvent;
    }

    private void OnAfterSceneLoadedEvent()
    {
        inputControl.Gameplay.Enable();
    }

    private void OnLoadEvent(GameSceneSO arg0, Vector3 arg1, bool arg2)
    {
        inputControl.Gameplay.Disable();
    }

    

    private void OnDisable()
    {
        inputControl.Disable();
        sceneloadEvent.LoadRequestEvent -= OnLoadEvent;
        afterSceneLoadedEvent.onEventRaised -= OnAfterSceneLoadedEvent;
        loadDataEvent.onEventRaised -= OnLoadDataEvent;
        backToMenuEvent.onEventRaised -= OnLoadDataEvent;
    }

    private void OnLoadDataEvent()
    {
        isDead = false;
    }

    private void Update()
    {
        inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();//调用inputsystem设定好的move函数读取二维向量值
        CheckState();
    }

    private void FixedUpdate()//与物理有关的帧更新
    {
        if(!isHurt&&!isAttack)
           Move();
    }


    public void Move()
    {
        rb.velocity = new Vector2(inputDirection.x*speed*Time.deltaTime,rb.velocity.y);//修改刚体的速度，模拟原来的下坠重力速度

        int faceDir = (int)transform.lossyScale.x;
        if (inputDirection.x > 0)
        {
            faceDir = 1;
        }
        if (inputDirection.x <0)
        {
            faceDir = -1;
        }
        transform.localScale = new Vector3(faceDir, 1, 1);
    }

    private void Jump(InputAction.CallbackContext context)
    {
        if (physicsCheck.isGround)
        {
            rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
            Debug.Log("正在跳跃");
            GetComponent<AudioDefination>()?.PlayAudioClip();
        }
    }
    
    private void Attack(InputAction.CallbackContext context)
    {
        playerAnimation.PlayerAttack();
        isAttack = true;
    }

    public void GetHurt(Transform attacker)
    {
        isHurt = true;
        rb.velocity = Vector2.zero;
        Vector2 dir = new Vector2((transform.position.x - attacker.position.x), 0).normalized;

        rb.AddForce(dir * hurtForce, ForceMode2D.Impulse);
    }

    public void PlayerDead()
    {
        isDead = true;
        inputControl.Gameplay.Disable();
    }

    public void CheckState()
    {
        coll.sharedMaterial = physicsCheck.isGround ? normal : wall;
    }
}